﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Devonline.Core;

namespace Devonline.Entity
{
    /// <summary>
    /// 字符串类型的默认基类
    /// </summary>
    public abstract class EntitySetWithCreate : EntitySetWithCreate<string>
    {
    }

    /// <summary>
    /// 带新增的基础数据模型抽象基类
    /// </summary>
    public abstract class EntitySetWithCreate<TKey> : EntitySet<TKey>, IEntitySetWithCreate<TKey> where TKey : IConvertible
    {
        /// <summary>
        /// 行版本号
        /// </summary>
        [Column("row_version"), DisplayName("行版本号"), ConcurrencyCheck, MaxLength(36)]
        public virtual TKey RowVersion { get; set; }
        /// <summary>
        /// 数据状态
        /// </summary>
        [Column("state", TypeName = "VARCHAR(16)"), DisplayName("数据状态"), DefaultValue(DataState.Available), Excel]
        public virtual DataState State { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        [Column("created_on"), DisplayName("创建时间"), Excel]
        public virtual DateTime? CreatedOn { get; set; }
        /// <summary>
        /// 创建人
        /// </summary>
        [Column("created_by"), DisplayName("创建人"), MaxLength(36), Excel]
        public virtual string CreatedBy { get; set; }

        /// <summary>
        /// 创建方法
        /// </summary>
        public virtual void Create(string createdBy = null)
        {
            Id ??= KeyGenerator.GetKey<TKey>();
            RowVersion = Id;
            CreatedOn ??= DateTime.UtcNow;
            CreatedBy ??= createdBy ?? AppSettings.USER_SYSTEM;
        }
    }
}
